package com.asking.Controller;

import com.asking.domain.Answer;
import com.asking.service.AnswerService;
import com.asking.vo.Result;
import com.asking.vo.ResultVo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author: 宋定鑫
 * @Date: 2018/10/17 18:25
 */
@RestController
@RequestMapping("/answer")
public class AnswerController {

    @Resource
    private AnswerService answerService;

    @PostMapping("/add")
    public ResponseEntity<Result> add(Answer answer){
        try{
            answerService.add(answer);
            return new ResponseEntity<>(new Result(200,"回答新增成功"), HttpStatus.OK);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity<>(new Result(500,"回答新增失败"),HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    @PutMapping
    public ResponseEntity<Result> updatAnswer(Answer answer){
        try{
            answerService.updata(answer);
                return new ResponseEntity<>(new Result(204,"回答修改成功"), HttpStatus.NO_CONTENT);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity<>(new Result(500,"回答修改失败"),HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<Result> idAnswer(@PathVariable("id")Integer id){
        try {
            Answer answer =answerService.selectByAnswer(id);
            return new ResponseEntity<>(new Result(200,"",answer),HttpStatus.OK);
        }catch (Exception e){
            return new ResponseEntity<>(new Result(500,""),HttpStatus.INTERNAL_SERVER_ERROR);

        }

    }

    @GetMapping("/findAll")
    public ResponseEntity<Result> findAll(Integer page,Integer rows){
        try {
            ResultVo all = answerService.findAll(page,rows);
            return new ResponseEntity<>(new Result(200,"",all),HttpStatus.OK);
        }catch (Exception e){
            return new ResponseEntity<>(new Result(500,""),HttpStatus.INTERNAL_SERVER_ERROR);

        }

    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Result> deleteidanswer(@PathVariable("id")Integer id){
        try{
            answerService.deleteidanswer(id);
            return new ResponseEntity<>(new Result(204,"回答删除成功"),HttpStatus.NO_CONTENT);
        }catch(Exception e){
            e.printStackTrace();
            return new ResponseEntity<>(new Result(500,"回答删除失败"),HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }
}
